package com.jnu.myaccount_drawer.ui.manage;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jnu.myaccount_drawer.R;
import com.jnu.myaccount_drawer.data.AccountType;
import com.jnu.myaccount_drawer.data.DataBank;

import java.util.List;

public class WechatActivity extends AppCompatActivity {

    private DataBank dataBankWechat;
    private List<AccountType> wechatAccount;
    private double netAssetsWechat;

    ActivityResultLauncher<Intent> launcherDetail = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {

    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wechat);

        initData();

        //账户余额
        TextView textViewNetAssetsPriceWechat=findViewById(R.id.text_view_net_assets_price_wechat);
        for(int i=0;i<wechatAccount.size();i++)
        {
            netAssetsWechat+=wechatAccount.get(i).getPrice();
        }
        textViewNetAssetsPriceWechat.setText(netAssetsWechat+"");

        Button buttonBack=findViewById(R.id.button_back);
        buttonBack.setOnClickListener(view -> finish());

        RecyclerView wechatRecyclerView=findViewById(R.id.recycler_view_items_wechat);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        wechatRecyclerView.setLayoutManager(layoutManager);

        wechatRecyclerView.setAdapter(new WechatRecyclerViewAdapter(wechatAccount));
    }

    private void initData() {
        dataBankWechat = new DataBank(WechatActivity.this);
        wechatAccount= dataBankWechat.loadData();
        for(int i=0;i<wechatAccount.size();i++)
        {
            if(!wechatAccount.get(i).getPayType().equals("微信"))
            {
                wechatAccount.remove(i);
                i--;
            }
        }
    }

    private class WechatRecyclerViewAdapter extends RecyclerView.Adapter {
        private final List<AccountType> wechatAccount;

        public WechatRecyclerViewAdapter(List<AccountType> wechatAccount) {
            this.wechatAccount=wechatAccount;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.account_holder_history, parent, false);

            return new WechatViewHolder(view);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            WechatViewHolder wechatViewHolder = (WechatViewHolder) holder;
            wechatViewHolder.getImageView().setImageResource(wechatAccount.get(position).getPictureId());
            wechatViewHolder.getTextViewType().setText(wechatAccount.get(position).getType());
            wechatViewHolder.getTextViewPrice().setText(wechatAccount.get(position).getPrice() +"");
            wechatViewHolder.getTextViewDate().setText(wechatAccount.get(position).getDate());
            wechatViewHolder.getTextViewPayType().setText(wechatAccount.get(position).getPayType());

            wechatViewHolder.itemView.setOnClickListener(view -> {
                //Toast.makeText(CashActivity.this,"dianjile",Toast.LENGTH_LONG).show();
                Intent intent=new Intent(WechatActivity.this, DetailActivity.class);
                intent.putExtra("position",holder.getAdapterPosition());
                intent.putExtra("pictureId", wechatAccount.get(holder.getAdapterPosition()).getPictureId());
                intent.putExtra("type", wechatAccount.get(holder.getAdapterPosition()).getType());
                intent.putExtra("price", wechatAccount.get(holder.getAdapterPosition()).getPrice());
                intent.putExtra("payOrIncome", wechatAccount.get(holder.getAdapterPosition()).getPayOrIncome());
                intent.putExtra("payType", wechatAccount.get(holder.getAdapterPosition()).getPayType());
                intent.putExtra("date", wechatAccount.get(holder.getAdapterPosition()).getDate());
                launcherDetail.launch(intent);
            });
        }

        @Override
        public int getItemCount() { return wechatAccount.size();}

        private class WechatViewHolder extends RecyclerView.ViewHolder {
            private ImageView imageView;
            private TextView textViewType;
            private TextView textViewPrice;
            private TextView textViewPayType;
            private TextView textViewDate;

            public WechatViewHolder(View itemView) {
                super(itemView);

                this.imageView=itemView.findViewById(R.id.image_view_account);
                this.textViewType=itemView.findViewById(R.id.text_view_account_type);
                this.textViewPrice=itemView.findViewById(R.id.text_view_account_price);
                this.textViewPayType=itemView.findViewById(R.id.text_view_pay_type);
                this.textViewDate=itemView.findViewById(R.id.text_view_date_number);
            }
            public ImageView getImageView() {
                return imageView;
            }

            public TextView getTextViewType() {
                return textViewType;
            }

            public TextView getTextViewPrice() {
                return textViewPrice;
            }

            public TextView getTextViewPayType() {
                return textViewPayType;
            }

            public TextView getTextViewDate() {
                return textViewDate;
            }
        }
    }
}